﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Easily
{
    public partial class frmCountLoop : Form
    {
        #region 属性
        private GeneralIcon MyIcon = null;
        private string countNum = "";
        //【2021/4/27】
        private string varName = "";//记录循环次数的变量名
        private string oldVarName = " ";//记录checkBoxSelectVariableName之前选择的变量名
        private string oldVar = " ";//记录checkBoxSelectVariable之前选择的变量名
        /// <summary>
        /// 循环次数：可为数字或变量
        /// </summary>
        public string CountNum
        {
            get { return this.countNum; }
            set
            {
                this.countNum = value;
                //this.MyIcon.code.InsertCode = 
                if (value != "")
                {
                    if (value[0] >= '0' && value[0] <= '9')//第一个字符是数字，说明不是变量
                    {
                        this.checkBoxValue.Checked = true;
                        this.numericUpDownCountLoop.Enabled = true;
                        this.numericUpDownCountLoop.Value = Convert.ToUInt16(value);
                    }
                    else
                    {
                        this.checkBoxSelectVariable.Checked = true;
                        this.comboBoxSelectVariable.Enabled = true;
                        //[2021/4/27]原先加入了没有变量的处理，但是这里会在窗体初始化之前执行，所以变量加入了它也不知道
                        //所以删除这里的处理
                        this.comboBoxSelectVariable.Text = value;
                        //int indexOfVariable = this.comboBoxSelectVariable.Items.IndexOf(value);
                        //if (indexOfVariable != -1)
                        //{
                        //    this.comboBoxSelectVariable.SelectedIndex = indexOfVariable;
                        //    this.checkBoxSelectVariable.Checked = true;
                        //    this.comboBoxSelectVariable.Text = value;
                        //}
                        //else
                        //{
                        //    this.comboBoxSelectVariable.Text = "";
                        //    MessageBox.Show("原选择的变量已经不存在,可能已经被删除，请重新设置!", "重新设置", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        //}                 
                    }
                    //countLoopI原来的确定循环变量都切换为varname
                    //uint16_t  + varName + " = 0; "去除，变量需要在外面定义赋值
                    this.MyIcon.code.InsertCode = "for(; " + varName
                        + " < " + value + ";" + varName + "++)\r\n{";
                    this.MyIcon.code.Comment = "循环次数：" + value;
                }
            }
        }
        #endregion
        public frmCountLoop(GeneralIcon icon)
       {           
            InitializeComponent();
            MyIcon = icon;
            int i;
            //(1) 清空下拉框内容
            comboBoxSelectVariable.Items.Clear();
            //【20210427】添加comboBoxSelectVariableName，该变量用于记录
            comboBoxSelectVariableName.Items.Clear();

            //(2) 向下拉框和Variables中添加程序中的变量的名字
            if (Globaldefine.FrmParent.ActivedrmGraphicFlow != null)
            {
                ////添加模块局部变量201512
                //for (i = 0; i < Globaldefine.FrmParent.ActivedrmGraphicFlow.ModuleVariable.Count; i++)
                //{
                //    comboBoxSelectVariable.Items.Add(Globaldefine.FrmParent.ActivedrmGraphicFlow.ModuleVariable[i]);
                //    comboBoxSelectVariableName.Items.Add(Globaldefine.FrmParent.ActivedrmGraphicFlow.ModuleVariable[i]);
                //}
                //添加系统变量
                for (i = 0; i < Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableSys.Count; i++)
                {
                    comboBoxSelectVariable.Items.Add(Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableSys[i]);
                    comboBoxSelectVariableName.Items.Add(Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableSys[i]);
                }
                //局部变量
                foreach (var item in Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableLocal)
                {
                    comboBoxSelectVariable.Items.Add(item);
                    comboBoxSelectVariableName.Items.Add(item);
                }
            }
            comboBoxSelectVariable.Items.Add(" ");
            comboBoxSelectVariableName.Items.Add(" ");
            if (MyIcon.code.Save != "")
                restore();           
        }

        /// <summary>
        /// 从保存信息字符串中恢复控件
        /// </summary>
        private void restore()
        {
            string[] temps = MyIcon.code.Save.Split(';');
            varName = temps[0];
            comboBoxSelectVariableName.Text = varName;
            //txt_VarName.Text = varName;
            CountNum = temps[1];
        }
        ///==============================================================================

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (checkBoxValue.Checked)
                CountNum = numericUpDownCountLoop.Value.ToString();
            else if (checkBoxSelectVariable.Checked)
            {
                if(comboBoxSelectVariable.SelectedItem!=null)
                    CountNum = comboBoxSelectVariable.SelectedItem.ToString();
            }
            MyIcon.code.Save = varName + ";" + CountNum;
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
             this.Close();
        }

        /// <summary>
        /// 直接设置循环次数的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxValue_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxValue.Checked)
            {
                checkBoxSelectVariable.Checked = false;
                numericUpDownCountLoop.Enabled = true;
                comboBoxSelectVariable.Enabled = false;
            }
            else
            {
                numericUpDownCountLoop.Enabled = false;
            }
        }

        /// <summary>
        /// 选择变量模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxSelectVariable_CheckedChanged(object sender, EventArgs e)
        { 
            if (checkBoxSelectVariable.Checked)
            {
                checkBoxValue.Checked = false;
                numericUpDownCountLoop.Enabled = false;
                comboBoxSelectVariable.Enabled = true;
            }
        }

        /// <summary>
        /// 修改注释
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddNote_Click(object sender, EventArgs e)
        {
            using (frmNodeEditer NE = new frmNodeEditer(MyIcon.code.Comment))
            {
                if (NE.ShowDialog() == DialogResult.OK)
                {
                    MyIcon.code.Comment = NE.NoteEdit;
                }
            }
        }

        /// <summary>
        /// 清空注释
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClearNote_Click(object sender, EventArgs e)
        {
            MyIcon.code.Comment = string.Empty;
        }

        //private void txt_VarName_TextChanged(object sender, EventArgs e)
        //{
        //    varName = txt_VarName.Text;
        //}

        /// <summary>
        /// 【2021/4/27】
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxSelectVariable_SelectedIndexChanged(object sender, EventArgs e)
        {
            //（1）如果之前选过变量，并且comboBoxSelectVariableName中没有该变量
            if (oldVar != " " && !comboBoxSelectVariableName.Items.Contains(oldVar))
                comboBoxSelectVariableName.Items.Add(oldVar);
            //(2)再将现在选择的变量记录过去
            oldVar = comboBoxSelectVariable.Text;
            //(3)删除另一个下拉框中的该变量，不可以重复选择，没有意义
            if (oldVar != " ")
                comboBoxSelectVariableName.Items.Remove(oldVar);
        }

        /// <summary>
        /// 【2021/4/27】
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxSelectVariableName_SelectedIndexChanged(object sender, EventArgs e)
        {
            //(1)如果之前选过变量，并且comboBoxSelectVariable中没有该变量
            if (oldVarName != " " && !comboBoxSelectVariable.Items.Contains(oldVarName))
                comboBoxSelectVariable.Items.Add(oldVarName);
            //(2)再将现在选择的变量记录过去
            varName = comboBoxSelectVariableName.Text;
            oldVarName = varName;
            //(3)删除另一个下拉框中的该变量，不可以重复选择，没有意义
            if(oldVarName != " ")
                comboBoxSelectVariable.Items.Remove(varName);
        }
    }
}